package com.example.administrator.pandanotes;

import android.content.ContentValues;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.litepal.crud.DataSupport;

import java.util.List;
/**
 * Created by 于博 on 2018-07-13.
 * 书籍修改界面
 * 有修改按钮，点击修改书籍
 */
public class BookUpdateActivity extends AppCompatActivity {
    //变量***************************************************************************
    private Button btnUpdate = null;
    private TextView tVBookISBN = null;
    private TextView tVBookImgID = null;
    private EditText eTBookName = null;
    private EditText eTBookAuthor = null;
    private EditText eTBookPub = null;
    private EditText eTBookClass = null;
    private  Books b;
        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_update);
            //初始化及绑定**************************************************************
            eTBookClass = (EditText) findViewById(R.id.eTUpdateBookClassName); //绑定书籍文本框
            tVBookISBN = (TextView) findViewById(R.id.tVBooksUpdateISBN); //绑定ISBN
            eTBookName = (EditText) findViewById(R.id.eTUpdateBookName);//绑定书名文本框
            eTBookAuthor = (EditText) findViewById(R.id.eTUpdateBookAuthor);//绑定作者文本框
            eTBookPub = (EditText) findViewById(R.id.eTUpdateBookPub);   //绑定出版社文本框
            btnUpdate = (Button) findViewById(R.id.btnUpdateBook);//绑定更新按钮
            tVBookImgID = (TextView)findViewById(R.id.tVUpdateBookImgId);//绑定图片id
            //得到书籍
            b = (Books)getIntent().getSerializableExtra("Book");
            Log.d("书籍ISBN:",b.getBook_ISBN());
            //显示值
            eTBookClass.setText(b.getClass_name());
            Log.d("书籍更新书籍类显示:",""+b.getClass_name());
            tVBookISBN.setText(b.getBook_ISBN());
            eTBookName.setText(b.getBook_name());
            eTBookAuthor.setText(b.getBook_author());
            eTBookPub.setText(b.getBook_press());
            tVBookImgID.setText(b.getBook_imageId());
            //添加点击事件***********************************************************
            btnUpdate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {//修改按钮点击事件，进行书籍信息更新
                    //获得值
                    String ClassName = eTBookClass.getText().toString();
                    String BookName = eTBookName.getText().toString();
                    String BooKAuthor = eTBookAuthor.getText().toString();
                    String BookPress = eTBookPub.getText().toString();
                    //异常处理
                    if(BooKAuthor.length()==0)
                    {
                        BooKAuthor="暂无";
                    }
                    if (BookPress.length()==0)
                    {
                        BookPress="暂无";
                    }
                    List<BookClass> BookClass = DataSupport.where("class_name=?",ClassName).find(BookClass.class);//找到书籍类中所填
                    if (BookClass.size()<=0)
                    {
                        Toast.makeText(BookUpdateActivity.this,"无此书籍类",Toast.LENGTH_SHORT).show();
                    }
                    else if(BookName.length()<1||BookName.length()>20)//审核书籍名
                    {
                        Toast.makeText(BookUpdateActivity.this,"书籍名应1-20位",Toast.LENGTH_SHORT).show();
                    }
                    else if(BooKAuthor.length()<1||BooKAuthor.length()>20)//审核作者名
                    {
                        Toast.makeText(BookUpdateActivity.this,"作者名应1-20位",Toast.LENGTH_SHORT).show();
                    }
                    else if(BookPress.length()<1||BookPress.length()>20)//审核出版社名
                    {
                        Toast.makeText(BookUpdateActivity.this,"出版社应1-20位",Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        ContentValues values = new ContentValues();
                        values.put("class_name",ClassName);
                        Log.d("书籍更新,获得书籍类:",ClassName);
                        values.put("book_name",BookName);
                        values.put("book_press",BookPress);
                        values.put("book_author",BooKAuthor);
                        DataSupport.updateAll(Books.class, values, "book_ISBN = ?", b.getBook_ISBN());
                        Toast.makeText(BookUpdateActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(BookUpdateActivity.this,BooksActivity.class);
                        startActivity(intent);
                    }
                }
            });
    }
    //返回键调用函数，进行上一页*************************************************************
    @Override
    public void onBackPressed()
    {
        this.finish(); //关闭当前Activity
    }
}
